﻿(function($) {

  // BBCode only supports a small subset of HTML, so remove
  // any toolbar buttons that are not currently supported.
  $.cleditor.defaultOptions.controls =
	  "undo redo | bold italic underline superscript | size style | removeformat" +
	  " | bullets numbering | justify center | cut copy paste pastetext | source";

  // Save the previously assigned callback handlers
  var oldAreaCallback = $.cleditor.defaultOptions.updateTextArea;
  var oldFrameCallback = $.cleditor.defaultOptions.updateFrame;

  // Wireup the updateTextArea callback handler
  $.cleditor.defaultOptions.updateTextArea = function(html) {

    // Fire the previously assigned callback handler
    if (oldAreaCallback)
      html = oldAreaCallback(html);

    // Convert the HTML to BBCode
    return $.cleditor.convertHTMLtoLatex(html);

  }

  // Wireup the updateFrame callback handler
  $.cleditor.defaultOptions.updateFrame = function(code) {

    // Fire the previously assigned callback handler
    if (oldFrameCallback)
      code = oldFrameCallback(code);

    // Convert the BBCode to HTML
    return $.cleditor.convertLatexToHTML(code);

  }

  // Expose the convertHTMLtoLatex method
  $.cleditor.convertHTMLtoLatex = function(html) {
	  
	    $.each([
	            [/(.*?)<div(.*?)/i, "$1\\\\<div$2"],
	            [/<div><br><\/div>/gi, "\n\n"], // a
	            [/<\/div><div.*?>(.*?)<\/div>(.*?)/gi, "<\/div>\\\\$1$2"], // 1,2
	            [/\n<div.*?>(.*?)<\/div>(.*?)/gi, "\n$1$2"], // 3,4
	            [/<div.*?>(.*?)<\/div>/i, "$1"], // 5,6
	            [/(.*?)<div.*?>(.*?)<\/div>/gi, "$1\\\\$2"], //7,8
	            [/<b>(.*?)<\/b>/gi, "\\textbf{$1}"],
	            [/<i>(.*?)<\/i>/gi, "\\textit{$1}"],
	            [/<sup>(.*?)<\/sup>/gi, "\\up{$1}"],
	            [/<u>(.*?)<\/u>/gi, "\\underline{$1}"],
	            [/<ul>/gi, "\\begin{itemize}"],
	            [/<\/ul>/gi, "\\end{itemize}"],
	            [/<ol>/gi, "\\begin{enumerate}"],
	            [/<\/ol>/gi, "\\end{enumerate}"],
	            [/<li>/gi, "\\item "],
	            [/<\/li>/gi, ""],
	            [/<.*?>(.*?)<\/.*?>/g, "$1"],
	            [/&nbsp;/g, " "],
	            [/&/g, "\&"],
	            [/<\/.*?>/g, ""],
	            [/\\\\\n\n/g, "\n\n"],
	            [/\\\\\\\\/g, "\\"],
	            [/<.*?>/g, ""]
	            ], function(index, item) {
	              html = html.replace(item[0], item[1]);
	            });

    return html;

  }

  // Expose the convertLatexToHTML method
  $.cleditor.convertLatexToHTML = function(code) {

    $.each([
      [/\r/g, ""],
      [/\\\\/g, "<br />"],
      [/\\textbf{(.*?)}/gi, "<b>$1</b>"],
      [/\\textit{(.*?)}/gi, "<i>$1</i>"],
      [/\\underline{(.*?)}/gi, "<u>$1</u>"],
      [/\\up{(.*?)}/gi, "<sup>$1<\/sup>"],
      [/\\item(.*?)\\end{itemize}/gi, "</li><li>$1</li>\\end{itemize}"],
      [/\\item(.*?)\\item/gi, "<li>$1</li><li>"],
      [/\\begin{itemize}/gi, "<ul>"],
      [/\\end{itemize}/gi, "</ul>"],
      [/\\begin{enumerate}/gi, "<ol>"],
      [/\\end{enumerate}/gi, "</ol>"],
      [/\n\n/g, "<div><br></div>"]
      ], function(index, item) {
        code = code.replace(item[0], item[1]);
      });

    return code;

  }

})(jQuery);